<?php
class Lead_EDO_Scaffold
{
    
    /**
     * Object to scaffold
     *
     * @var Lead_EDO_Object_Abstract
     */
    private $_obj;
    
    
    public function __construct(Lead_EDO_Object_Abstract $obj)
    {
        $this->_obj = $obj;
    }
    
    
    
    public function render()
    {
        
        foreach($this->_obj->getFieldObjects() as $key => $fieldObj) {
            
            if($fieldObj instanceof Lead_EDO_Datatype_Field_Value) {
	            if(!in_array($fieldObj->getName(), array('id', 'dimension_id'))) {
	                print "<label>{$fieldObj->getName()}</label>\n";
	                print "<input id=\"scaffold_{$fieldObj->getName()}\" name=\"{$fieldObj->getName()}\" type=\"text\" value=\"{$fieldObj->getValue()}\" />\n";
	            }
            } else {

                if($fieldObj->getType() == 'BelongsTo') {
                    print "<label>{$fieldObj->getName()}</label>\n";

                    
                    $finder = $fieldObj->getOwner()->getHandler()->getFinder($fieldObj->getRelatedClass());
                    
                    $order = $fieldObj->getConfig('order');
                    
                    $keyField = $fieldObj->getForeignKey();
                                                            
                    $options = $finder->findAll(array(), $order); 
                                        
                    print "<select id=\"{$fieldObj->getName()}\"";

                    print "onchange=\"$('scaffold_{$keyField}').value = this.value\"";
                    print ">";
                    print "<option value=\"\">null</option>";
                    foreach($options as $optionObj) {
                        print "<option value=\"{$optionObj->id}\"";
                        if($this->_obj->$keyField == $optionObj->id) print " selected=\"selected\"";
                        print ">{$optionObj}</option>\n";
                    }
                    
                    print "</select>";
                    
                }
                
                
                
            }
            
        }
        
        
            
            
            
                
        
        
    }
    
    
    public function insert($darray)
    {
        foreach($this->_obj->getFieldObjects() as $fieldObj) {
            
            if(key_exists($fieldObj->getName(), $darray)) {
                
                if(!$fieldObj->isNotNull && $darray[$fieldObj->getName()] == "")
                    $darray[$fieldObj->getName()] = null;
                
                
                $fieldObj->setValue($darray[$fieldObj->getName()]);
            }
                        
        }

        $this->_obj->insert();

        try {
            $this->_obj->getHandler()->commit();
        } catch(Exception $e) {
            return false;
        }
        
        return true;
        
    }
    
    
    
    
    public function save($darray)
    {
        foreach($this->_obj->getFieldObjects() as $fieldObj) {
            
            if(key_exists($fieldObj->getName(), $darray)) {
                                
                if(!$fieldObj->isNotNull && $darray[$fieldObj->getName()] == "")
                    $darray[$fieldObj->getName()] = null;
                
                $fieldObj->setValue($darray[$fieldObj->getName()]);
            }
            
        }

        $this->_obj->save();

        try {
            $this->_obj->getHandler()->commit();
        } catch(Exception $e) {
            return false;
        }
        
        return true;
        
    }
    
       
    
    
    
}
